package concurent;

import lombok.extern.slf4j.Slf4j;
import thread.ThreadStatu;

/**
 * @author ganbo
 * @date 2022/10/18 19:45
 */

@Slf4j
public class ConcurrentDemo1 {


    static int counter = 0;
    static final Object room = new Object();


    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (room) {
                    counter++;
                }
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (room) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    counter--;
                }
            }
        }, "t2");

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        log.debug("{}", counter);

    }
}
